
/**    
 * <p>Copyright： 东软烟草产品研发团队</p>   
 */

package com.jkl.service.dept;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jkl.service.dept.inter.BSDeptServiceInterface;
import com.jkl.dao.dept.BSDeptMapper;
import com.jkl.entity.dept.BSDept;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

/**
 * <p>
 * 123service
 * <p>
 * <p>
 * 123service
 * <p>
 * <p>
 * 变更说明：lin.pf 2018年1月17日 123service
 * <p>
 * Date:2018年10月07日
 * 
 * @author lin.pf
 * @version 1.0
 */
@Service
@Slf4j
public class BSDeptService implements BSDeptServiceInterface {

	@Autowired
	private BSDeptMapper bSDeptMapper;

	@Override
	public List<BSDept> queryParamByGroupCode(Integer deptId) {
		Example example1 = new Example(BSDept.class);
		example1.createCriteria().andEqualTo("rowId", deptId);
		Example example = new Example(BSDept.class);
		List<BSDept> dept = bSDeptMapper.selectByExample(example1);
		if (dept.get(0).getLevel() == 3) {
			example.createCriteria().andEqualTo("rowId", dept.get(0).getParentId());
		} else {
			Example.Criteria a = example.createCriteria();
			a.andEqualTo("parentId", deptId).andEqualTo("level", 2);
			Example.Criteria b = example.createCriteria();
			b.orEqualTo("rowId", deptId).andEqualTo("level", 2);
			example.or(b);
		}
		return bSDeptMapper.selectByExample(example);
	}

	@Override
	public List<BSDept> getDeptById(Integer rowId, Integer parentId) {
		Example example = new Example(BSDept.class);
		if (rowId == null)
			example.createCriteria().andEqualTo("parentId", parentId);
		else
			example.createCriteria().andEqualTo("parentId", parentId).andEqualTo("rowId", rowId);
		return bSDeptMapper.selectByExample(example);
	}

	@Override
	public BSDept getAreaByDeptId(String deptId) {
		// 先查询当前deptid的部门信息
		BSDept orgDept = bSDeptMapper.selectByPrimaryKey(Integer.parseInt(deptId));
		if( 3 == orgDept.getLevel()){
			//3 为县级,县级根据parentid 查询返回市局信息
		 orgDept = bSDeptMapper.selectByPrimaryKey(orgDept.getParentId());
		}
		return orgDept;
	}

	@Override
	public List<BSDept> getDeptBylevel(int level) {
		return bSDeptMapper.getDeptBylevel(level);
	}

	@Override
	public void updateDept(BSDept dept) {
		if (dept.getLevel() == 2){
			dept.setRowIdEnd(dept.getRowId()+99999999);
		}
		if (dept.getLevel() == 3){
			dept.setDeptCode(dept.getRowId() / 10000000 + "");
			dept.setRowIdEnd(dept.getRowId()+9999999);
		}else if(dept.getLevel() == 4){
			dept.setDeptCode(dept.getRowId() / 100000 + "");
			dept.setRowIdEnd(dept.getRowId()+99999);
		}else if (dept.getLevel() == 5){
			dept.setDeptCode(dept.getRowId() / 1000 + "");
			dept.setRowIdEnd(dept.getRowId()+999);
		}else if (dept.getLevel() == 6){
			dept.setDeptCode(dept.getRowId() + "");
			dept.setRowIdEnd(dept.getRowId());
		}
		bSDeptMapper.updateByPrimaryKey(dept);
	}

	@Override
	public void addDept(BSDept dept) {
		if (dept.getLevel() == 3){
			dept.setDeptCode(dept.getRowId() / 10000000 + "");
			dept.setRowIdEnd(dept.getRowId()+9999999);
		}else if(dept.getLevel() == 4){
			dept.setDeptCode(dept.getRowId() / 100000 + "");
			dept.setRowIdEnd(dept.getRowId()+99999);
		}else if (dept.getLevel() == 5){
			dept.setDeptCode(dept.getRowId() / 1000 + "");
			dept.setRowIdEnd(dept.getRowId()+999);
		}else if (dept.getLevel() == 6){
			dept.setDeptCode(dept.getRowId() + "");
			dept.setRowIdEnd(dept.getRowId());
		}
		bSDeptMapper.insert(dept);
	}

	@Override
	public void deleteDept(BSDept dept) {
		bSDeptMapper.deleteByPrimaryKey(dept);
	}

	@Override
	public List<BSDept> queryByParentId(String parentId) {
		return bSDeptMapper.queryByParentId(parentId);
	}

	@Override
	public BSDept getAreaByAreaId(String deptId) {
		return bSDeptMapper.selectByPrimaryKey(Integer.parseInt(deptId));
	}


	@Override
	public BSDept getAreaByDeptCode(String deptCode) {
		return bSDeptMapper.getAreaByDeptCode(deptCode);
	}


	@Override
	public List<BSDept> getDeptList() {
		return bSDeptMapper.getDeptList();
	}

	@Override//获取单个
	public List<BSDept> getDeptObject(String rowId) {
		return bSDeptMapper.getDeptObject(rowId);
	}

	@Override
	public BSDept getDeptObjectlevel(String cityName) {
		return bSDeptMapper.getDeptObjectlevel(cityName);
	}
	@Override//地址--查询全部
	public List<BSDept> queryAddress(Integer parentId) {
		return bSDeptMapper.queryAddress(parentId);
	}
	
	@Override
	public List<BSDept> getDeptByRoleType(int roleType) {
		Example example = new Example(BSDept.class);
		if (roleType < 3 ) {
			example.createCriteria().andLessThan("level", 3);
		} else if (roleType == 3){
			example.createCriteria().andEqualTo("level", 2);
		} else if (roleType == 4) {
			example.createCriteria().andEqualTo("level", 3);
		}
		return bSDeptMapper.selectByExample(example);
	}
	@Override
	public List<BSDept> getBSDeptStreet(String deptId) {
		return bSDeptMapper.getBSDeptStreet(deptId);
	}
	@Override
	public List<BSDept> getBSDeptCommunity(String deptId) {
		return bSDeptMapper.getBSDeptCommunity(deptId);
	}
	@Override
	public List<BSDept> getTree(int level) {
		List<BSDept> a = bSDeptMapper.getTree(level);
		return a;
	}

	@Override
	public List<BSDept> getDeptObjectLevel4(String rowId) {
		return bSDeptMapper.getDeptObjectLevel4(rowId);
	}

	@Override
	public List<BSDept> getChildrenListByParentId(Integer parentId) {
		return bSDeptMapper.getChildrenListByParentId(parentId);
	}

}